var e = require("../@babel/runtime/helpers/interopRequireDefault"),
  n = e(require("../@babel/runtime/helpers/classCallCheck")),
  t = e(require("../@babel/runtime/helpers/createClass")),
  i = require("config.js").config,
  a = require("uuid-manager.js"),
  r = wx.getAccountInfoSync().miniProgram.appId,
  s = (function () {
    function e() {
      return (
        (0, n.default)(this, e),
        (this.channelId = void 0),
        (this.keyInfo = void 0),
        e.instance || (this._init(), (e.instance = this)),
        e.instance
      );
    }
    return (
      (0, t.default)(
        e,
        [
          {
            key: "_init",
            value: function () {
              try {
                (this.channelId = parseInt(wx.getStorageSync(i.channel))),
                  this.channelId || (this.channelId = 0);
              } catch (e) {
                this.channelId = 0;
              }
              var e = !0;
              try {
                (this.keyInfo = wx.getStorageSync(i.keyInfo)),
                  this.keyInfo
                    ? this._isKeyExpired(this.keyInfo, 5)
                      ? (this.keyInfo = "")
                      : (e = !1)
                    : (this.keyInfo = "");
              } catch (e) {
                this.keyInfo = "";
              }
              e && this._pullAndSave();
            },
          },
          {
            key: "getKeyInfo",
            value: function () {
              return (
                this._isKeyExpired(this.keyInfo) && this._pullAndSave(),
                Object.assign({}, this.keyInfo)
              );
            },
          },
          {
            key: "getChannelId",
            value: function () {
              return this.channelId;
            },
          },
          {
            key: "_isKeyExpired",
            value: function (e) {
              var n =
                  arguments.length > 1 && void 0 !== arguments[1]
                    ? arguments[1]
                    : 1,
                t = Math.floor(Date.now() / 1e3);
              return Math.abs(t - e.expiredDate) >= e.validTime * n;
            },
          },
          {
            key: "_pullAndSave",
            value: function () {
              var e = this;
              this._isChannelIdInValid()
                ? this._pullChannelId()
                    .then(function () {
                      e._pullSignKey();
                    })
                    .catch(function (e) {})
                : this._pullSignKey();
            },
          },
          {
            key: "_isChannelIdInValid",
            value: function () {
              return 0 === this.channelId || isNaN(this.channelId);
            },
          },
          {
            key: "_pullSignKey",
            value: function () {
              var e = this;
              this._reqUserSignKeyFromServer(this.channelId)
                .then(function (n) {
                  var t = n.resp;
                  0 === t.ret && e._saveKeyInfo(t);
                })
                .catch(function (e) {});
            },
          },
          {
            key: "_pullChannelId",
            value: function () {
              var e = this;
              return new Promise(function (n, t) {
                e._getChannelIdFromServer()
                  .then(function (i) {
                    var a = i.resp;
                    0 === a.ret ? (e._saveChannelId(a), n()) : t(a.ret);
                  })
                  .catch(function (e) {
                    t(e);
                  });
              });
            },
          },
          {
            key: "_saveKeyInfo",
            value: function (e) {
              var n = Math.floor(Date.now() / 1e3);
              (this.keyInfo = e),
                (this.keyInfo.expiredDate = n + this.keyInfo.validTime),
                wx.setStorage({ key: i.keyInfo, data: this.keyInfo });
            },
          },
          {
            key: "_saveChannelId",
            value: function (e) {
              (this.channelId = e.channelId),
                wx.setStorage({ key: i.channel, data: this.channelId });
            },
          },
          {
            key: "_reqUserSignKeyFromServer",
            value: function (e) {
              var n = {
                fin: { apn: 0, authType: 0, guid: a.uuid, netType: 0 },
                req: { userId: "", channelId: e },
              };
              return this._requestQQPimServer(n, i.reqUserSignKeyCMD);
            },
          },
          {
            key: "_getChannelIdFromServer",
            value: function () {
              var e = {
                fin: { apn: 0, authType: 0, guid: a.uuid, netType: 0 },
                req: { appId: r },
              };
              return this._requestQQPimServer(e, i.getChannelIdCMD);
            },
          },
          {
            key: "_requestQQPimServer",
            value: function (e, n) {
              var t = this;
              return new Promise(function (a, r) {
                wx.request({
                  url: i.baseUrl + n,
                  data: e,
                  header: { "Content-Type": "application/json" },
                  method: "POST",
                  success: function (e) {
                    if (t._isHttpSuccess(e.statusCode)) {
                      var n = e.data;
                      0 == n.ret ? a(n.data) : r(n);
                    } else r(e.statusCode);
                  },
                  fail: function (e) {
                    r(e);
                  },
                });
              });
            },
          },
          {
            key: "_isHttpSuccess",
            value: function (e) {
              return (e >= 200 && e < 300) || 304 === e;
            },
          },
        ],
        [
          {
            key: "getInstance",
            value: function () {
              return this.instance || (this.instance = new e()), this.instance;
            },
          },
        ],
      ),
      e
    );
  })();
module.exports = { KeyManager: s };
